import React from 'react';
import { View, Text, TouchableOpacity, Platform,FlatList,DeviceEventEmitter} from 'react-native';
import moment from 'moment'
import navigationhelper from 'src/components/navigationhelper'
import {CalendarList, LocaleConfig} from 'react-native-calendars';
import NavBar from 'src/components/navBar'
import Layer from 'src/components/Layer'
import {isIPhoneX,deviceWidth} from 'src/util'
import ScrollableTabView from 'react-native-scrollable-tab-view'
import { deviceHeight } from '../../../../util';
import  RepayMentRecordCell from './repayMentRecordCell'
import {inject, observer} from "mobx-react/native"
import  userHome from '../../../../service/Home/index'


@inject('userAuth')

@observer

export default class repayAllMentRecordList extends React.Component {

  refreshing = false;
  hadMore = false;
  pageSize = 20;
  pageIndex = 1;

    constructor(props) {
      super(props);
      this.state={
        dataArry:[]
      }
    }
    componentDidMount(){
      Layer.hiddenLoading()
      this.getDataList()
    }
    SeparatorView(){
        return(<View style={{height:10,width:deviceWidth}}/>)
    }

    renderFooter = ()=> {
      if (this.hadMore) {
          return (
              <View style={{
                  height:36,
                  backgroundColor:'white',
                  justifyContent: 'center',
                  alignItems: 'center'
              }}>
                  <Text>{'正在加载....'}</Text>
              </View>
          )
      } else {
          return (
              <View style={{
                  height:36,
                  backgroundColor:'white',
                  justifyContent:'center',
                  alignItems: 'center'
              }}>
                  <Text>{'暂无更多'}</Text>
              </View>
          )
      }
  }
//继续执行
  // goOnContinuePlan =(plan_id)=>{
  //   Layer.showLoading()
  //   userHome.fetchPlanContinue({
  //     token:this.props.userAuth.token,
  //     plan_id:plan_id
  //   }).then((res)=>{
  //     Layer.hiddenLoading()
  //     if(res.status == 'success'){
  //       DeviceEventEmitter.emit('refreshCardManager')
  //       Layer.showMsg('执行成功')
  //       this.getDataList();
  //     }
  //   }).catch((res)=>{
  //     Layer.hiddenLoading()
  //     Layer.showMsg(res.message)
  //   })
  // }


  canCeldataPlanList =(plan_id)=>{
    userHome.fetchCancelPlanList({
      token: this.props.userAuth.token,
      plan_id:plan_id
    }).then((res)=>{
      if(res.status == 'success'){
        this.getDataList()
      }
    }).catch((res)=>{
      Layer.showMsg(res.message)
    })
  }
    getDataList=()=>{
      Layer.showLoading()
      if(this.props.status > 0)
      {
        userHome.fetchPlanDataList({
          token: this.props.userAuth.token,
          page:this.pageIndex,
          page_size:this.pageSize,
          status:this.props.status
        }).then((res)=>{
          Layer.hiddenLoading()
          DeviceEventEmitter.emit('refreshCardManager')
          this.refreshing = false;
          if(res.status == 'success'){
            let rows = res.data.list;
            if (this.pageIndex === 1) {
              this.setState({dataArry:rows});
          } else {
              this.setState({dataArry:this.state.dataArry.concat(rows)});
          }
          this.hadMore = (rows.length === this.pageSize);
          }
        }).catch((res)=>{
          Layer.hiddenLoading()
          this.refreshing = false;
        })
      }
      else{
        userHome.fetchPlanDataList({
          token: this.props.userAuth.token,
          page:this.pageIndex,
          page_size:this.pageSize,
        }).then((res)=>{
          Layer.hiddenLoading()
          DeviceEventEmitter.emit('refreshCardManager')
          this.refreshing = false;
          if(res.status == 'success'){
            let rows = res.data.list;
            if (this.pageIndex === 1) {
              this.setState({dataArry:rows});
          } else {
              this.setState({dataArry:this.state.dataArry.concat(ref.Result.Rows)});
          }
          this.hadMore = (ref.Result.Rows.length === this.pageSize);
          }
        }).catch((res)=>{
          Layer.hiddenLoading()
          this.refreshing = false;
        })
      }
    }
    ListEmptyComponent=()=> {
        return(
          <View style={{ height: deviceHeight *0.5,alignItems: 'center', justifyContent: 'center' }}>
          <Text style={{fontSize:25,color:'#8E8E93',fontWeight:'bold'}}>暂无数据</Text>
         </View>
        )
    }
    
    _renderRefresh = () => {
      this.pageIndex = 1;
      this.refreshing = true;
      this.getDataList();
    }
    _onEndReached = () => {
      if (!this.refreshing && this.hadMore) {
          this.pageIndex++;
          this.getDataList();
      }
  }

    render(){

        return(
        <View style={{flex:1, padding:10}}>
          <FlatList
            ListEmptyComponent={this.ListEmptyComponent}
            onRefresh={this._renderRefresh}
            onEndReached={this._onEndReached}
            //  ListFooterComponent={this.renderFooter}//尾巴
            refreshing={this.refreshing}
            onEndReachedThreshold={0.1}
            data={this.state.dataArry}
            keyExtractor={(item, index) =>index}
            ItemSeparatorComponent={this.SeparatorView}
            renderItem={({item}) =>
            {
              return(
              <RepayMentRecordCell  cancelpayDetail={(data)=>{
                  this.canCeldataPlanList(item.id) 
              }} 
              itemData={item}
              goContinuePalan={()=>{
                this.goOnContinuePlan(item.id) 
              }}
              />
              )
            }
          }/>
        </View>
        ) 
    }
 }